package org.hales.service;

import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.hales.dao.deYang.PetrolStationMapper;
import org.hales.pojo.FuelGun;
import org.hales.pojo.PetrolStation;
import org.hales.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 陈建康
 */
//@Service
public class PetrolStationService {
//    @Autowired
    PetrolStationMapper petrolStationMapper;
    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(PetrolStationService.class);

    /**
     * 保存加油枪基础信息
     * @param file excel文件
     */
    public void saveFuelGum(MultipartFile file, int sheetIndex, int headIndex) {
        List<Map<String, Object>> collect = ExcelUtil.readExcelFile(file,sheetIndex,headIndex);
        List<FuelGun> list = collect.stream().map(map -> ExcelUtil.convert(map, FuelGun.class)).collect(Collectors.toList());
        list.forEach(item -> {
            try {
                petrolStationMapper.insertFuelGunInfo(item);
            } catch (Exception e) {
                log.error("加油枪信息：{}，异常{}", JSONObject.toJSONString(item), e.getMessage());
                throw new RuntimeException(e);
            }
        });
    }
    /**
     * 保存加油站基础信息
     * @param file excel文件
     */
    public void savePetrolStation(MultipartFile file, int sheetIndex, int headIndex) {
        List<Map<String, Object>> collect = ExcelUtil.readExcelFile(file,sheetIndex,headIndex);
        List<PetrolStation> list = collect.stream().map(map -> ExcelUtil.convert(map, PetrolStation.class))
                .filter(item -> StringUtils.isNotBlank(item.getMnCode()))
                .collect(Collectors.toList());
        list.forEach(item -> {
            try {
                petrolStationMapper.insertPetrolStationInfo(item);
            } catch (Exception e) {
                log.error("加油站信息：{}，异常{}", JSONObject.toJSONString(item), e.getMessage());
                throw new RuntimeException(e);
            }
        });
    }
}
